package com.mao.spring.cloud.factory;

import com.mao.spring.cloud.api.EmployeeRemoteService;
import com.mao.spring.cloud.entity.Employee;
import com.mao.spring.cloud.util.ResultEntity;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 实现consumer服务降级的功能
 * 实现FallbackFactory传入feign注解标记的接口类型
 * 调用provider失败的时候，就会调用这个对象对应的方法（相当于伪造了一个接口的实现类）
 * @author maomeng
 * @create 2021-06-16- 15:35
 */
@Component
public class MyFallBackFactory implements FallbackFactory<EmployeeRemoteService> {

    /**
     *
     * @param cause 失败原因对应的异常对象
     * @return 一个新的接口实现
     */
    @Override
    public EmployeeRemoteService create(Throwable cause) {
        return new EmployeeRemoteService() {
            @Override
            public Employee getEmployeeRemote() {
                return null;
            }

            @Override
            public List<Employee> getEmpListRemote(String keyword) {
                return null;
            }

            @Override
            public ResultEntity<Employee> getEmp(String signal) {
                return ResultEntity.failed(cause.getMessage() + "降级生效");
            }
        };
    }
}
